using System;
using System.Collections.Generic;
using System.Text;
using DAL;
using System.Web;
using System.Data;

namespace BO
{
    public class Settings
    {
        public class Keys
        {
            public const string EMAIL = "email";
            public const string TEL = "tel";
            public const string COPYRIGHT = "copyright";
            public const string CONTACT_EMAIL = "contactemail";
            public const string CONTACT_SMTP = "contactsmtp";
            public const string CONTACT_PORT = "contactport";
            public const string HTML_OUTPUT_ONE_LINE = "htmlouputoneline";
        }

        private static Dictionary<string, object> _SettingsDictionary = new Dictionary<string, object>();

        public static Dictionary<string, object> SettingsDictionary
        {
            set { _SettingsDictionary = value; }
            get
            {
                string cachename = "SettingsDictionary";
                object fromcache = HttpContext.Current.Cache.Get(cachename);
                if (fromcache != null)
                {
                    _SettingsDictionary = (Dictionary<string, object>)fromcache;
                }
                else
                {
                    DataTable dt;
                    using (MainDB db = new MainDB())
                    {
                        dt = db.StoredProcedures.Settings_GetAll();
                    }
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            _SettingsDictionary.Add(row["Key"].ToString(), row["Value"]);
                        }
                        HttpContext.Current.Cache.Insert(cachename, _SettingsDictionary, null, DateTime.Now.AddDays(1), TimeSpan.Zero);
                    }
                }
                return _SettingsDictionary;
            }
        }

        public static object Get(string Key)
        {
            if (SettingsDictionary.ContainsKey(Key)) return SettingsDictionary[Key];
            else return null;
        }

        public static string GetString(string Key)
        {
            return Get(Key).ToString();
        }

        public static bool GetBoolean(string Key)
        {
            bool result = false;
            try
            {
                result = Convert.ToBoolean(Get(Key));
            }
            catch { }
            return result;
        }

        public static int GetInt(string Key)
        {
            int result = 0;
            try
            {
                result = Convert.ToInt32(Get(Key));
            }
            catch { }
            return result;
        }

        public static void UpdateKey(string Key, object Value)
        {
            if (Value != null)
            {
                using (MainDB db = new MainDB())
                {
                    db.StoredProcedures.Settings_Update(Key, Value.ToString());
                }
            }
        }
    }
}
